﻿<%@ WebHandler Language="C#" Class="Getmsg" %>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
using System.Xml;
using BusinessLogic.ClassFactory;
using LitJson;
using RichECMS;

public class Getmsg : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
        Hashtable result = new Hashtable();
        //string sctxt = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request["sc"]);
        string pno = HttpContext.Current.Request["pno"] ?? "0";
        string pcount = HttpContext.Current.Request["pcount"] ?? "20";
        int pageno = 0;
        int pagecount = 20;
        if (IsNum(pno))
        {
            pageno = int.Parse(pno);
        }
        if (IsNum(pcount))
        {
            pagecount = int.Parse(pcount);
        }

        StringBuilder sbWhere = new StringBuilder();

        /*  if (sctxt.Trim() != "")
         {
             sbWhere.AppendFormat(" and Article.chvTitle like '%{0}%'", sctxt);
         } */
        sbWhere.Append("and Article.chvTitle");
        List<Hashtable> reslist = new List<System.Collections.Hashtable>();
        try
        {
            using (ArticleFactory objFactory = new ArticleFactory())
            {
                StringBuilder strWhere = new StringBuilder();
                strWhere.Append("select Article.*  from Article inner  join ArtSecRelation on Article.intArticleID = ArtSecRelation.intArticleID where 1=1 and  ArtSecRelation.intSectionID=21 order by Article.dtmInTime desc,Article.intArticleID desc");
                /*
                strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.* from Article inner  join ArtSecRelation on Article.intArticleID = ArtSecRelation.intArticleID and Article.intState = '" + Request["type"].ToString().Trim().Replace("'","")+ "' " + " where ArtSecRelation.intSectionID=" + State.intSTID.ToString() + State.strWhereSql + " order by Article.intSort desc ,Article.dtmInTime desc,Article.intArticleID desc)t order by intSort asc, dtmInTime asc ,intArticleID asc)m " + " order by intSort asc, dtmInTime asc ,intArticleID asc)mm  order by intSort desc, dtmInTime desc ,intArticleID desc");
                */
                BusinessLogic.BusinessRules.Article objArticle = objFactory.CreatorByFullSql(strWhere.ToString());
                if (objArticle != null)
                {
                    result["total"] = (objArticle.Count % pagecount) == 0 ? (objArticle.Count / pagecount) : (objArticle.Count / pagecount + 1);
                    result["current"] = pageno;
                    for (int i = 0; i < objArticle.Count; i++)
                    {
                        if (i >= pageno * pagecount && i < (pageno + 1) * pagecount)
                        {
                            Hashtable temp = new Hashtable();
                            temp["aid"] = objArticle.DataItem.intArticleID;
                            temp["atitle"] = objArticle.DataItem.chvTitle;
                            temp["info"] = objArticle.DataItem.chvSummary;
                            temp["content"] = objArticle.DataItem.chvDescription;
                            temp["answer"] = objArticle.DataItem.txtContent;
                            temp["atime"] = objArticle.DataItem.dtmInTime.ToString("MM-dd");
                            reslist.Add(temp);
                        }
                        objArticle.SelectRow(i + 1);
                    }

                }

            }

        }
        catch (Exception error)
        {
            result["error"] = error.Message.Replace("/r/n", "");
        }
        result["list"] = reslist;
        context.Response.Write(
           JsonMapper.ToJson(result));
    }

    /// <summary>
    /// 检测是否能转化为数字
    /// </summary>
    /// <param name="param">检测字符串</param>
    /// <returns>输出true/false</returns>
    public static bool IsNum(string param)
    {
        const string regEx = @"^[0-9]*$";
        if (System.Text.RegularExpressions.Regex.IsMatch(param, regEx))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}